#! /usr/bin/perl

# Copyright (c) 2009, 2010, 2011 Nicira, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

use strict;
use warnings;

my ($scale) = 1;
print ".PS\n";
print "linethick = 1;\n";
while (<>) {
    if (/^graph/) {
        (undef, $scale) = split;
    } elsif (/^node/) {
        my (undef, $name, $x, $y, $width, $height, $label, $style, $shape, $color, $fillcolor) = split;
        $x *= $scale;
        $y *= $scale;
        $width *= $scale;
        $height *= $scale;
        print "linethick = ", ($style eq 'bold' ? 0.5 : 1.0), ";\n";
        print "box at $x,$y wid $width height $height \"$name\"\n";
        if ($style eq 'bold') {
            my $inset = 2.0 / 72.0;
            $width -= $inset * 2;
            $height -= $inset * 2;
            print "box at $x,$y wid $width height $height\n";
        }
    } elsif (/edge/) {
        my (undef, $tail, $head, $n, $rest) = split(' ', $_, 5);
        my @xy;
        for (1...$n) {
            my ($x, $y);
            ($x, $y, $rest) = split(' ', $rest, 3);
            push(@xy, [$x * $scale, $y * $scale]);
        }
        my ($label, $xl, $yl);
        if (scalar(my @junk = split(' ', $rest)) > 2) {
            if ($rest =~ s/^"([^"]*)"\s+//) {
                $label = $1;
            } else {
                ($label, $rest) = split(' ', $rest, 2);
            }
            ($xl, $yl, $rest) = split(' ', $rest, 3);
            $xl *= $scale;
            $yl *= $scale;
        }
        my ($style, $color) = split(' ', $rest);

        print "linethick = ", ($style eq 'dotted' ? 0.5 : 1), ";\n";

        print "spline -> from $xy[0][0],$xy[0][1]";
        for (my ($i) = 0; $i <= $#xy; $i++) {
            print " to $xy[$i][0],$xy[$i][1]";
        }
        print "\n";

        print "\"$label\" at $xl,$yl\n" if defined($label);
    }

}
print ".PE\n";
